package flash3.net
{
	import flash.utils.ByteArray;
	
	public class ByteArrayPlus extends ByteArray
	{
		public function ByteArrayPlus()
		{
			super();
		}
		
		public function readString():String
		{
			var length:uint = readUnsignedInt();
			if(length != 0)
				return readMultiByte(length, "gb2312");
			else
				return "";
		}
		
		public function writeString(data:String, multiByte:Boolean = true):void
		{
			if (multiByte)
			{
				var byte:ByteArray = new ByteArray();
				byte.writeMultiByte(data, "gb2312");  	// 指定字符编码集
				writeUnsignedInt(byte.length);			// 长度
				writeBytes(byte);						// 内容
			}
			else
			{
				writeUnsignedInt(data.length);			// 长度
				if( data.length > 0 )
					writeUTFBytes(data);				// 内容
			}
		}
		
		public function clear():void
		{
			this.length = 0;
		}
	}
}